/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dialogEditor;

import dialogEditor.elements.DialogPhraseWidget;
import dialogEditor.elements.diaStructWidgetAbstact;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.ConnectProvider;
import org.netbeans.api.visual.action.ConnectorState;
import org.netbeans.api.visual.action.ReconnectProvider;
import org.netbeans.api.visual.action.WidgetAction;
import org.netbeans.api.visual.anchor.Anchor;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.anchor.AnchorShape;
import org.netbeans.api.visual.anchor.PointShape;
import org.netbeans.api.visual.border.BorderFactory;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.router.RouterFactory;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
import org.netbeans.modules.visual.experimental.widget.general.ListWidget;

/**
 *
 * @author sl_ru
 * 
 * 
 * Dialog structure editor. 
 * Used NetBeans API.
 */
public class diaStructEdit extends GraphScene.StringGraph
{
    //Main elements
    //VMDGraphScene sc;
    public JFrame frame;
    public LayerWidget mainLayer;
    public LayerWidget connectionLayer;
    public LayerWidget interractionLayer;


    public WidgetAction connectAction;
    public WidgetAction reconnectAction;
    public WidgetAction moveAction;
    public WidgetAction resizeAction;
    
    public diaStructEdit me;

   public static final String MOVE_TOOL = "moveTool";
    
    public diaStructEdit()
    {   
        super();
        me = this;
        initScene();
        
        
    }
        
    private void initScene() //init scene, layer and etc
    {
        
        setBackground(Color.DARK_GRAY);
        getActions ().addAction (ActionFactory.createZoomAction ());
        getActions ().addAction (ActionFactory.createPanAction ());
        
        mainLayer = new LayerWidget(this);
        addChild(mainLayer);
        mainLayer.setBackground(Color.DARK_GRAY);

        connectionLayer = new LayerWidget(this);
        addChild(connectionLayer);
        
        interractionLayer = new LayerWidget(this);
        addChild(interractionLayer);
        
        connectAction = ActionFactory.createConnectAction (interractionLayer, new SceneConnectProvider ());
        reconnectAction = ActionFactory.createReconnectAction (new SceneReconnectProvider ());
        moveAction = ActionFactory.createMoveAction();
        resizeAction = ActionFactory.createResizeAction();
        
        

        DialogPhraseWidget d = new DialogPhraseWidget(me, "fdsh");
        mainLayer.addChild(d);
        d.setPreferredLocation(new Point(43, 100));
        
        d = new DialogPhraseWidget(me, "fdsdfgsh");
        mainLayer.addChild(d);
        
        Show(false);
    }

    @Override
    protected Widget attachNodeWidget(String n)
    {
        return null;
    }

    @Override
    protected Widget attachEdgeWidget(String e)
    {
        return null;
    }

    @Override
    protected void attachEdgeSourceAnchor(String e, String n, String n1)
    {    }

    @Override
    protected void attachEdgeTargetAnchor(String e, String n, String n1)
    {    }
    
    
    
    private class SceneConnectProvider implements ConnectProvider //CONNECTER
    {

        private String source = null;
        private String target = null;

        public boolean isSourceWidget(Widget sourceWidget)
        {
            if (sourceWidget instanceof diaStructWidgetAbstact)
            {
                diaStructWidgetAbstact w = (diaStructWidgetAbstact) sourceWidget;

                return w.canBeSource;
            } else
            {
                return false;
            }
        }

        public ConnectorState isTargetWidget(Widget sourceWidget, Widget targetWidget)
        {
            if (targetWidget instanceof diaStructWidgetAbstact)
            {
                diaStructWidgetAbstact t = (diaStructWidgetAbstact) targetWidget;
                diaStructWidgetAbstact s = (diaStructWidgetAbstact) sourceWidget;
                
                
                return (t.canBeTarget && !s.parent.equals(t.id)) ? ConnectorState.ACCEPT : ConnectorState.REJECT_AND_STOP;
            } 
            else
            {
                return ConnectorState.REJECT_AND_STOP;
            }
        }

        public boolean hasCustomTargetWidgetResolver(Scene scene)
        {
            return false;
        }

        public Widget resolveTargetWidget(Scene scene, Point sceneLocation)
        {
            return null;
        }

        public void createConnection(Widget sourceWidget, Widget targetWidget)
        {
            ConnectionWidget connection = new ConnectionWidget (me);
            connection.setTargetAnchorShape (AnchorShape.TRIANGLE_FILLED);
            connection.setEndPointShape (PointShape.SQUARE_FILLED_BIG);
            connection.getActions ().addAction (createObjectHoverAction ());
            connection.getActions ().addAction (createSelectAction ());
            connection.setTargetAnchor(AnchorFactory.createDirectionalAnchor(targetWidget, AnchorFactory.DirectionalAnchorKind.VERTICAL));
            connection.setSourceAnchor(AnchorFactory.createDirectionalAnchor(sourceWidget, AnchorFactory.DirectionalAnchorKind.HORIZONTAL));
            connection.setRouter(RouterFactory.createOrthogonalSearchRouter(mainLayer));

            connection.getActions ().addAction (reconnectAction);
            connectionLayer.addChild (connection);
        }
    }

    private class SceneReconnectProvider implements ReconnectProvider //RECONNECTER 
    {

        String edge;
        String originalNode;
        String replacementNode;

        public void reconnectingStarted(ConnectionWidget connectionWidget, boolean reconnectingSource)
        {
        }

        public void reconnectingFinished(ConnectionWidget connectionWidget, boolean reconnectingSource)
        {
        }

        public boolean isSourceReconnectable(ConnectionWidget connectionWidget)
        {
            return originalNode != null;
        }

        public boolean isTargetReconnectable(ConnectionWidget connectionWidget)
        {
            return originalNode != null;
        }

        public ConnectorState isReplacementWidget(ConnectionWidget connectionWidget, Widget replacementWidget, boolean reconnectingSource)
        {
            if (replacementNode != null)
            {
                return ConnectorState.ACCEPT;
            }
            return true ? ConnectorState.REJECT_AND_STOP : ConnectorState.REJECT;
        }

        public boolean hasCustomReplacementWidgetResolver(Scene scene)
        {
            return false;
        }

        public Widget resolveReplacementWidget(Scene scene, Point sceneLocation)
        {
            return null;
        }

        public void reconnect(ConnectionWidget connectionWidget, Widget replacementWidget, boolean reconnectingSource)
        {

        }
    }
    
    public void Show(boolean visibility) //show and initialize window
    {
        if(frame == null)
        {
            JScrollPane panel = new JScrollPane (createView());
            panel.getHorizontalScrollBar ().setUnitIncrement (32);
            panel.getHorizontalScrollBar ().setBlockIncrement (256);
            panel.getVerticalScrollBar ().setUnitIncrement (32);
            panel.getVerticalScrollBar ().setBlockIncrement (256);

            int width=1000,height=700;

            frame = new JFrame();
            frame.add (panel, BorderLayout.CENTER);
            frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
            java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            frame.setBounds((screenSize.width-width)/2, (screenSize.height-height)/2, width, height);
            frame.setVisible (visibility);
        }
        else
        {
            frame.setVisible(visibility);
        }
    }
    
} 
